package br.usp.icmc.biomac.client.view.lixo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Data model of dicom results, that are displayed in the DicomResultGrid class.
 *
 * @author amsnyder
 */
public class DicomResultModel {
    private static DicomResultModel ourInstance = new DicomResultModel();

    private DicomResultGridImpl dicomResultGrid;

//    private static EPadLogger log = EPadLogger.getLogger();

    public static DicomResultModel getInstance() {
        return ourInstance;
    }

    public void addDicomResultGrid(DicomResultGridImpl resultGrid){
        dicomResultGrid = resultGrid;
    }

    private DicomResultModel() {
    }

    /**
     * Clear the model.
     */
    public void clear(){
        //dicomResultGrid.clear();
    }

    public void parseSeriesResponse(String response, String studyId){
        //log.debug("Series response: "+response);

        String[] lines = response.split("\n");
        String[] labels = lines[0].split(",");

        //parse the data into a table.
        List<String> keys = parseKeys(labels);
        List<Map<String,String>> rowList = new ArrayList<Map<String, String>>();
        for(int i=1;i<lines.length;i++){
            Map<String,String> rowData = parseData(keys,lines[i].split(","));
            rowList.add(rowData);
        }

        //(optionally) sort the rowList.

        //find the appropriate row to insert this data after.
        int rowIndex = dicomResultGrid.getRowIndexForStudy(studyId);
        dicomResultGrid.showSeriesDataForStudy(studyId);

        int addedRowCount = lines.length-1;
        for(int i=0; i<addedRowCount; i++){
            dicomResultGrid.setRowSeriesData(rowIndex+i,rowList.get(i));
        }

    }

    /**
     * Parse the response from the DicomProxy.
     * @param response
     */
    public void parseStudyResponse(String response){
        String[] lines = response.split("\n");
        String[] labels = lines[0].split(",");

        int rows = lines.length;
        dicomResultGrid.resizeRows(rows);

        List<String> keys = parseKeys(labels);
        for(int i=1;i<rows;i++){
            Map<String,String> rowData = parseData(keys,lines[i].split(","));
            dicomResultGrid.setRowStudyData(i,rowData);
        }//for
    }

    /**
     * The response has comma separated keys on the first line.
     * @param firstLine
     * @return
     */
    public List<String> parseKeys(String[] firstLine){
        List<String> retVal = new ArrayList<String>(firstLine.length);
        for(String curr : firstLine){
            retVal.add(curr.trim());
        }
        return retVal;
    }

    /**
     *
     * @param keys
     * @param rowData
     * @return
     */
    public Map<String,String> parseData(List<String> keys, String[] rowData){
        Map<String,String> retVal = new HashMap<String,String>(rowData.length);
        int i=0;
        for(String curr : rowData){
            retVal.put(keys.get(i),curr.trim());
            i++;
        }
        return retVal;
    }

}
